﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core;
using Cofe.Core.EntryType;
using Cofe.Core.Implements;
using Cofe.Core.Property;

namespace Cofe.Data
{
    /// <summary>
    /// For use in ORM.
    /// </summary>
    public class DatabaseEntry 
    {
        public DatabaseEntry()
        {
            Implementations = new List<CofeSystemInfo>();
            HashTags = new List<HashTag>();
            ExtendedProperties = new List<PropertyPair>();
            LastCacheTime = DateTime.UtcNow;
            LastListTime = CofeConstants.MinDateTime;
        }

        public Guid Id { get; set; }
        public string ParseName { get; set; }
        public string Label { get; set; }
        public string OriginalType { get; set; }
        public virtual DatabaseEntry EntryTypeInfo { get; set; }
        /// <summary>
        /// Used by IEntryLink
        /// </summary>
        public virtual DatabaseEntry LinkedEntry { get; set; }

        public string Description { get; set; }
        public string SupportedProperties { get; set; }
        public string SupportDefinitions { get; set; }
        public string DefaultEntryType { get; set; }
        public DateTime LastCacheTime { get; set; }
        public DateTime LastListTime { get; set; }

        public virtual DatabaseEntry Parent { get; set; }
        public virtual List<DatabaseEntry> Children { get; set; }
        public virtual List<HashTag> HashTags { get; set; }
        
        public virtual Guid? ParentId { get; set; }
        public virtual Guid? EntryTypeInfoId { get; set; }
        public virtual Guid? LinkedEntryId { get; set; }

        public int Position { get; set; }

        /// <summary>
        /// Properties thats to be cached but not mapped to ORM.
        /// </summary>
        public List<PropertyPair> ExtendedProperties { get; set; }
        public List<CofeSystemInfo> Implementations { get; set; }


        public bool ListExpired
        {
            get { return LastListTime.Add(CofeServices.CofeSettings.ListExpireTime) < DateTime.UtcNow; }
        }

        public bool CacheExpired
        {
            get { return LastCacheTime.Add(CofeServices.CofeSettings.CacheExpireTime) < DateTime.UtcNow; }
        }

        public override string ToString()
        {
            return "[DatabaseEntry] " + ParseName;
        }

        public override int GetHashCode()
        {
            return ParseName.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            return obj is DatabaseEntry && this.GetHashCode() == obj.GetHashCode();
        }
    }
}
